cc.Class({
    extends: cc.Component,

    properties: {
        bubble: {
            default: null, type: cc.Prefab
        },
        speed: {
            default: 10,
            displayName: "Player speed"
        },
        move: false,
        moveX: null,
        moveY: null,
        hook_ani: {
            default: null,
            type: cc.Node
        }
    },

    // use this for initialization
    onLoad: function () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onKeyDown: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.j:
                this.hook_ani.getComponent(cc.Animation).play();
                break;
            case cc.macro.KEY.k:
                var bubbleNode = cc.instantiate(this.bubble);
                bubbleNode.getComponent("Bubble").game = this.node.parent.getComponent("Game");
                bubbleNode.getComponent("Bubble").player = this;
                this.node.parent.addChild(bubbleNode);
                bubbleNode.setPosition(cc.v2(this.node.x, this.node.y));
                bubbleNode.getComponent("Bubble").fire("a");
                break;

        }
    },
    // onKeyUp: function (event) {
    //     switch (event.keyCode) {
    //         case cc.macro.KEY.a:
    //             this.LEFT = false;
    //             break;
    //         case cc.macro.KEY.d:
    //             this.RIGHT = false;
    //             break;
    //         case cc.macro.KEY.w:
    //             this.UP = false;
    //             break;
    //         case cc.macro.KEY.s:
    //             this.DOWN = false;
    //             break;
    //     }
    // },
    // called every frame
    update: function (dt) {
        var x = 0;
        var y = 0;
        // if (this.LEFT) {
        //     x = x - this.speed;
        // }
        // if (this.RIGHT) {
        //     x = x + this.speed;
        // }
        // if (this.UP) {
        //     y = y + this.speed;
        // }
        // if (this.DOWN) {
        //     y = y - this.speed;
        // }

        // var x = this.speed*(Math.sin(2*Math.PI/360*(this.player.node.rotation)));
        // var y = this.speed*(Math.cos(2*Math.PI/360*(this.player.node.rotation)));

        if (this.move) {
            var rotation;
            var z = Math.sqrt(this.moveX*this.moveX+this.moveY*this.moveY);
            if(this.moveX>0 && this.moveY>0){
                rotation = Math.atan(this.moveX / this.moveY) * 180 / Math.PI;
            }else if(this.moveX>0 && this.moveY<0){
                rotation = Math.atan(this.moveX / this.moveY) * 180 / Math.PI+180;
            }else if(this.moveX<0 && this.moveY>0){
                rotation = Math.atan(this.moveX / this.moveY) * 180 / Math.PI;
            }else if(this.moveX<0 && this.moveY<0){
                rotation = Math.atan(this.moveX / this.moveY) * 180 / Math.PI+180;
            }
            this.node.angle = -rotation;
            this.node.runAction(cc.moveBy(0.1, cc.v2(this.moveX, this.moveY)));
        }
    },
});
